<template>
  <div>
    <!-- 搜索 -->
    <TopFilter ref="TopFilterss" :list="topFilterInit" :cols="3" :collapse="collapse" labelWidth="110" @filterChange="changeHandle"></TopFilter>
    <!-- table -->
    <FilterTable ref="table" :columns="columns" :params="fetchParams" :fetchapi="fetchapi" :datakey="'records'"  :onColumnsChange="columns => (this.columns = columns)" :isSelectColumn="false">
      <template slot="controls" slot-scope="props">
        <multiuse-button size="small" type="primary" icon="el-icon-plus" @click="exportFileBtn()">导出</multiuse-button>
      </template>
    </FilterTable>
  </div>
</template>
<script>
import moment from "moment";
import DealerList from '@/pages/claimDemage/tools/DealerList.vue';
import { getDestoryListByPage,excelExport,exporturl } from '@/api/usedPartsDestructionDealer/usedPartsToBeDestroyed';
import { exportExcelFile } from '@/utils'
import { getPartType } from '@/api/dictionary/index.js';
import { getAppId } from '@/assets/js/auth';

export default {
  components: {
    DealerList
  },
  data() {
    return {
      dataList: [],
      visible: false,
      visibleDrawer: false,
      collapse: true,
      infoId: null,
      topFilterInit: this.topFilterInitFun(),
      flterValue: {}, //参数
      columns: this.creatHeader(),
      // fetchApi: getUser,
      fetchParams: {xhrAbort: true}, //ipa 接口
      selectedRows: [], //选中经销商
      dialogTitle: '选择车辆',
      visibleAdd: false,
      fetchapi: getDestoryListByPage,
      dealer: {}
    };
  },
  created() {
    this.chuqi();
    this.fetchParams.beginApplyAt = moment().startOf('week').format('YYYY-MM-DD HH:mm:ss');
    this.fetchParams.endApplyAt = moment().endOf('week').format('YYYY-MM-DD HH:mm:ss');
  },
  methods: {
    close(){
      this.visible = false;
    },
    chooseOrderNo() {
      this.visibleAdd = true;
    },
    // 导出
    async exportFileBtn() {
      let json = Object.assign({}, { CODE: 'CODE_00000220' }, this.fetchParams);
      let res = await exporturl(json);
      if (res.resultCode == 200) {
        this.$router.push('/admin/auth/oem/downloadCenter');
      }
      // let beginSubmitDate = this.topFilterInit.find(x => x.fieldName == "beginApplyAt|endApplyAt").initialValue[0];
      // let endSubmitDate = this.topFilterInit.find(x => x.fieldName == "beginApplyAt|endApplyAt").initialValue[1];
      // if(!beginSubmitDate) {
      //   this.$notify.warning({ title: '提示信息', message: '索赔单审核开始日期不可为空！' });
      //   return
      // }
      // if(!endSubmitDate) {
      //   this.$notify.warning({ title: '提示信息', message: '索赔单审核结束日期不可为空！' });
      //   return
      // }
      // if(getAppId() == 'arcfox') {
      //   if (moment(moment(endSubmitDate).format('YYYY-MM-DD')).diff(moment(beginSubmitDate), 'months', true) > 12) {
      //     this.$notify.warning({ title: '提示信息', message: '索赔单审核日期跨度不可超过十二个月！' });
      //     return
      //   }
      // } else {
      //   if (moment(moment(endSubmitDate).format('YYYY-MM-DD')).diff(moment(beginSubmitDate), 'months', true) > 12) {
      //     this.$notify.warning({ title: '提示信息', message: '索赔单审核日期跨度不可超过十二个月！' });
      //     return
      //   }
      // }
      // let obj = Object.assign({}, this.fetchParams, { dtlStatusS: '55311011,55311012,55311013,55311015', ifReturn: 55001001,xhrAbort: false});
      // obj.beginApplyAt = this.topFilterInit.find(x => x.fieldName == "beginApplyAt|endApplyAt").initialValue[0];
      // obj.endApplyAt = this.topFilterInit.find(x => x.fieldName == "beginApplyAt|endApplyAt").initialValue[1];
      // await this.$confirm('是否将Excel表格保存至本地文件夹?', '提示', {
      //   confirmButtonText: '下载',
      //   cancelButtonText: '取消',
      //   type: 'warning',
      // })
      //   .then(async () => {
      //     let json = obj;
      //     await excelExport(json)
      //       .then((response) => {
      //         exportExcelFile(response.data, '待销毁旧件信息');
      //       })
      //       .catch((err) => {
      //         console.log(err);
      //       });
      //   })
    },
    add() {
      this.visibleAdd = true;
    },
    closeAdd() {
      this.visibleAdd = false;
    },
    saveAdd(val) {
      this.topFilterInit.find(x => x.fieldName == 'ownerCode').initialValue = val[0].dealerCode;
      this.dealer = {...val[0]};
      this.visibleAdd = false;
    },
    //搜索获取数据
    changeHandle(val) {
      if(!val.beginApplyAt) {
        this.$notify.warning({ title: '提示信息', message: '索赔单审核开始日期不可为空！' });
        return
      }
      if(!val.endApplyAt) {
        this.$notify.warning({ title: '提示信息', message: '索赔单审核结束日期不可为空！' });
        return
      }
      if(getAppId() == 'arcfox') {
        if (moment(moment(val.endApplyAt).format('YYYY-MM-DD')).diff(moment(val.beginApplyAt), 'months', true) > 12) {
          this.$notify.warning({ title: '提示信息', message: '索赔单审核日期跨度不可超过十二个月！' });
          return
        }
      } else {
        if (moment(moment(val.endApplyAt).format('YYYY-MM-DD')).diff(moment(val.beginApplyAt), 'months', true) > 12) {
          this.$notify.warning({ title: '提示信息', message: '索赔单审核日期跨度不可超过十二个月！' });
          return
        }
      }
      this.fetchParams = Object.assign({}, this.fetchParams, val , {ownerCode: this.dealer.dealerCode,xhrAbort: false});
    },
    async chuqi() {
      console.log("===== 获取下拉框数据 ====== ")
      const res = await getPartType({ type: 5501 });
      console.log("==========",res)
      if (res.resultCode == 200) {
        this.topFilterInit.find(x => x.fieldName == 'cmsClaimType').initialValue = '';
        this.topFilterInit.find(x => x.fieldName == 'cmsClaimType').itemList = res.data.map(y => ({ text: y.codeCnDesc, value: y.codeId }));
      }
    },
    //搜索数据
    topFilterInitFun() {
      return [
        {type: 'INPUT', label: '旧件代码', fieldName: 'partCode', placeholder: '请输入旧件代码...', initialValue: ''},
        {type: 'INPUT', label: '旧件名称', fieldName: 'partName', placeholder: '请输入旧件名称...', initialValue: ''},
        {
          type: 'SELECT',
          label: '索赔类型',
          fieldName: 'cmsClaimType',
          placeholder: '请输入索赔类型...',
          initialValue: '',
          itemList: []
        },
        {type: 'INPUT', label: '索赔单号', fieldName: 'claimNo', placeholder: '请输入索赔单号...', initialValue: ''},
        {type: 'RANGE_DATE', label: '索赔单审核时间', fieldName: 'beginApplyAt|endApplyAt', placeholder: '', initialValue: [
            moment()
              .startOf('week')
              .format('YYYY-MM-DD HH:mm:ss'),
            moment()
              .endOf('week')
              .format('YYYY-MM-DD HH:mm:ss')
          ],
          rules: [{ required: true, message: '请选择日期', trigger: 'change' }]
        },
        {type: 'INPUT', label: 'VIN', fieldName: 'vin', placeholder: '请输入VIN...', initialValue: ''}
      ];
    },
    //table表头和数据源
    creatHeader() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        { title: '索赔单号', dataIndex: 'claimNo', sorter: true },
        { title: '索赔类型', dataIndex: 'cmsClaimTypeStr', sorter: true },
        { title: '主损件代码', dataIndex: 'mainPartCode', sorter: true },
        { title: '主损件名称', dataIndex: 'mainPartName', sorter: true },
        { title: '旧件数量', dataIndex: 'partCount', sorter: true },
        { title: '待销毁数量', dataIndex: 'inventoryQuantity', sorter: true },
        { title: '索赔审核时间', dataIndex: 'checkAt', sorter: true },
        { title: 'VIN', dataIndex: 'vin', sorter: true },
        { title: '旧件代码', dataIndex: 'partCode', sorter: true },
        { title: '旧件名称', dataIndex: 'partName', sorter: true },
      ];
    }
  }
};
</script>

<style lang="less" scoped></style>
